﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using WP7Client.Classes;
using System.Collections.ObjectModel;
using WP7Client.Helpers;
using WP7Client.UserInterface.Pages;
using WP7Client.Managers;

namespace WP7Client.Controller
{
    public class ChatController : BaseController
    {
        #region Members

        private Conversation _Converstation;

        public ChatPage ChatPage;

        #endregion

        #region Get&Set

        public Conversation Conversation
        {
            get { return _Converstation; }
            set 
            { 
                _Converstation = value;

                if (Messages != null)
                {
                    foreach (ChatMessage cm in Messages)
                    {
                        cm.IsRead = true;
                    }
                }

                OnPropertyChanged("Conversation");
                OnPropertyChanged("Messages");
            }
        }

        public ObservableCollection<ChatMessage> Messages
        {
            get {
                if (Conversation != null)
                    return Conversation.Messages;
                else
                    return null;
            }
        }

        #endregion

        public ChatController()
        {

        }

        /// <summary>
        /// Versturen van een nieuwe chat bericht
        /// </summary>
        /// <param name="message">Het bericht</param>
        public void AddMessage(String message)
        {
            if (Conversation != null && ControllerHelper.ConversationsController != null)
            {
                lock (Conversation)
                {
                    Conversation.Messages.Add(new ChatMessage(message, false, DateTime.Now));
                    SendMessageManager.SendMessage(Conversation.ChatWithFriend, message);
                    OnPropertyChanged("Messages");
                }
            }
        }
    }
}
